package com.eastfair.auth.controller;

import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.auth.dto.*;
import com.eastfair.auth.entity.UserRole;
import com.eastfair.auth.service.UserRoleService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 用户角色表
 * </p>
 *
 * @author ligang
 * @date 2022-06-06
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/userRole")
@Api(value = "UserRole", tags = "用户角色表")
public class UserRoleController extends SuperController<UserRoleService, Long, UserRole, UserRolePageQuery, UserRoleSaveDTO, UserRoleUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<UserRole> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<UserRole> userRoleList = list.stream().map((map) -> {
            UserRole userRole = UserRole.builder().build();
            //TODO 请在这里完成转换
            return userRole;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(userRoleList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<UserRolePageQuery> params) {
        UserRolePageQuery pageQuery = params.getModel();
    }




    /**
     * 添加用户关联角色
     *
     * @param userRoleDto 用户角色实体
     * @return
     */
    @ApiOperation(value = "添加用户关联角色", notes = "添加用户关联角色")
    @PostMapping("/addUserRole")
    @SysLog("添加用户关联角色")
    public R addUserRole(@RequestBody UserRoleDto userRoleDto) {
        return baseService.addUserRole(userRoleDto.getUserIds(), userRoleDto.getRoleIds());
    }


    /**
     * 更新用户关联角色
     *
     * @param userRoleDto 用户角色实体
     * @return
     */
    @ApiOperation(value = "更新用户关联角色", notes = "更新用户关联角色")
    @PostMapping("/updUserRole")
    @SysLog("更新用户关联角色")
    public R updUserRole(@RequestBody UserRoleDto userRoleDto) {
        return baseService.updUserRole(userRoleDto.getUserIds(), userRoleDto.getRoleIds());
    }


    /**
     * 剔除用户关联角色
     *
     * @param userRoleDto 组织角色实体
     * @return
     */
    @ApiOperation(value = "剔除用户关联角色", notes = "剔除用户关联角色")
    @PostMapping("/removeUserRole")
    @SysLog("剔除用户关联角色")
    public R removeOrgRole(@RequestBody UserRoleDto userRoleDto) {
        return baseService.removeUserRole(userRoleDto.getUserIds(), userRoleDto.getRoleIds());
    }

    /**
     * 查询用户关联角色
     *
     * @param userRoleDto 组织角色实体
     * @return
     */
    @ApiOperation(value = "查询用户关联角色", notes = "查询用户关联角色")
    @PostMapping("/queryUserRole")
    @SysLog("查询用户关联角色")
    public R queryUserRole(@RequestBody UserRoleDto userRoleDto) {
        return R.success(baseService.list(null,userRoleDto.getUserIds(),userRoleDto.getRoleIds()));
    }
}
